<template>
  <div class="post-comment">
    <van-field
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      size="mini"
      @click="onPost"
      :disabled="!message"
    >发布</van-button>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  components: {},
  props: {
    articleId: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      message: '' // 输入框内容
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onPost () {
      this.$toast.loading({
        message: '发布中',
        forbidClick: true
      })
      const data = {
        id: this.articleId,
        comment: {
          message: this.message,
          user: this.user.nickname,
          photo: this.user.photo,
          time: new Date()
        }
      }
      await this.$http.put('api/tableTest/comment', data).then(({ data: res }) => {
        if (res.code === 0) {
          this.$emit('post-success', res.data[res.data.length - 1])
          this.$toast.success('发布成功')
          this.message = ''
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  padding: 14px;
  display: flex;
  align-items: center;
}
</style>
